package gui;

import entities.Artist;
import entities.MusicGenre;
import helpers.ValidationHelper;


// Διάλογος Διαχείρισης Καλλιτέχνη
public class ArtistForm extends javax.swing.JDialog {
    private String dialogResult = "Cancel";
    private final ValidationHelper validator = new ValidationHelper();
    
    public ArtistForm(Artist _entity) {
        initComponents();
        
        // Εμφάνισε το παράθυρο σαν modal και σε κέντρο σε σχέση με την parent form
        this.setModal(true);
        this.setLocationRelativeTo(this.getOwner());
        
        entity = _entity;
        
        updateControlsFromEntity();
    }
    
    // Constructor χωρίς ορίσματα που κάνει κλήση του παραπάνω constructor
    // με όρισμα νέο αντικείμενο
    public ArtistForm() {
        this(new Artist());
    }
    
    // Ενημέρωση των των controls από τα properties του entity
    private void updateControlsFromEntity(){
        // Αν το id του entity δεν είναι null, το αντικείμενο είναι persisted
        // οπότε αρχικοποιούμε τις τιμές των controls
        if (entity.getId() != null) {
            this.artisticNameText.setText(entity.getArtisticName());
            this.firstNameText.setText(entity.getFirstName());
            this.lastNameText.setText(entity.getLastName());
            this.sexComboBox.setSelectedItem(entity.getSex());        
            this.birthDateTextField.setText(validator.getDF().format(entity.getBirthDate()));
            this.birthPlaceTextField.setText(entity.getBirthPlace());
            this.musicGenreComboBox.setSelectedItem(entity.getMusicGenreId());
        }
    }
    
    // Ενημέρωση των properties του entity από τις τιμές των controls
    private void updateEntityFromControls() {
        entity.setFirstName(firstNameText.getText());
        entity.setLastName(lastNameText.getText());
        entity.setArtisticName(artisticNameText.getText());
        entity.setSex(sexComboBox.getSelectedItem().toString());
        entity.setBirthPlace(birthPlaceTextField.getText());
        entity.setMusicGenreId((MusicGenre)musicGenreComboBox.getSelectedItem());
        
        if (validator.isValidDate(this.birthDateTextField.getText())){
            entity.setBirthDate(validator.parseDate(this.birthDateTextField.getText()));
        }
    }
    
    // Επιστρέφει το entity της φόρμας
    public Artist getEntity()
    {
        updateEntityFromControls();
        return entity;
    }
    
    // Επιστρέφει την τρέχουσα τιμή του dialogResult (Save/Cancel)
    public String getDialogResult()
    {
        return this.dialogResult;
    }
    
    // Κλείνει το διάλογο αφού πρώτα κάνει τους απαραίτητους ελέγχους
    protected void closeDialog(String _dialogResult) {
        if (_dialogResult != null && !_dialogResult.isEmpty()){
            dialogResult = _dialogResult;
        }
        if ((this.dialogResult.equals("Save") && validateEntity()) || this.dialogResult.equals("Cancel")){
            this.dispose();
        }
    }
    
    // Έλεγχος εγκυρότητας δεδομένων πριν την αποθήκευση
    // με χρήση ενός managers.ValidationHelper
    protected boolean validateEntity(){
        validator.getRules().clear();
        validator.addDateRule(this.birthDateTextField.getText());
        
        return validator.validateRules(this);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entity = new entities.Artist();
        KLMRadioStationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("KLMRadioStationPU").createEntityManager();
        musicGenreQuery = java.beans.Beans.isDesignTime() ? null : KLMRadioStationPUEntityManager.createQuery("SELECT m FROM MusicGenre m");
        musicGenreList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : musicGenreQuery.getResultList();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        artisticNameText = new javax.swing.JTextField();
        firstNameText = new javax.swing.JTextField();
        lastNameText = new javax.swing.JTextField();
        sexComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        birthDateTextField = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        birthPlaceTextField = new javax.swing.JTextField();
        musicGenreComboBox = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Διαχείριση Καλλιτέχνη");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        saveButton.setText("Αποθήκευση");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Άκυρο");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Καλλιτεχνικό Όνομα");

        sexComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Άνδρας", "Γυναίκα" }));
        sexComboBox.setSelectedIndex(-1);

        jLabel2.setText("Όνομα");

        jLabel3.setText("Επίθετο");

        jLabel4.setText("Φύλο");

        jLabel5.setText("Ημερομηνία Γέννησης");

        birthDateTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));

        jLabel6.setText("Τόπος Γέννησης");

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicGenreList, musicGenreComboBox);
        bindingGroup.addBinding(jComboBoxBinding);

        jLabel7.setText("Είδος Μουσικής");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(musicGenreComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(birthPlaceTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(birthDateTextField)
                    .addComponent(sexComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lastNameText)
                    .addComponent(firstNameText)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(artisticNameText))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(artisticNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sexComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(birthDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(birthPlaceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(musicGenreComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        closeDialog("Save");
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        closeDialog("Cancel");
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        closeDialog("Cancel");
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager KLMRadioStationPUEntityManager;
    private javax.swing.JTextField artisticNameText;
    private javax.swing.JFormattedTextField birthDateTextField;
    private javax.swing.JTextField birthPlaceTextField;
    private javax.swing.JButton cancelButton;
    private entities.Artist entity;
    private javax.swing.JTextField firstNameText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField lastNameText;
    private javax.swing.JComboBox musicGenreComboBox;
    private java.util.List<entities.MusicGenre> musicGenreList;
    private javax.persistence.Query musicGenreQuery;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox sexComboBox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
